package zerous.taphero;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View.*;
import android.content.*;
import java.util.*;
import zerous.taphero.util.*;
import android.util.*;
import android.graphics.*;
import android.widget.AdapterView.*;

public class MainActivity extends Activity 
{
	Hero hero;
	ProgressBar pb;
	Button btn;
	TextView t, exP;
	ArrayAdapter storageAdapter;
	
	AlertDialog itemHandler;
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		
		hero = new Hero(getIntent().getStringExtra("heroName"));
		hero.init();
		
		pb = (ProgressBar)findViewById(R.id.mainProgressBar1);
		btn = (Button)findViewById(R.id.mainButton1);
		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				btnClick(v);
			}
		});
		
		
		t = (TextView)findViewById(R.id.mainTextView1);
		exP = (TextView)findViewById(R.id.mainTextView2);
		
		setTitle(hero.name);
    }

	@Override
	protected void onPause() {
		// TODO: Implement this method
		super.onPause();
		//Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
		hero.save();
		IOHelper.writeData(this, ".profile", CharacterSelectActivity.profile.getDataSrc().getBytes());
		
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		hero.init();
		
		t.setText("Level:"+hero.level);
		exP.setText("EXP:"+hero.exp+"/"+hero.needExp);
		pb.setProgress((int)((float)hero.exp/(float)hero.needExp*100));
		storageAdapter = new StorageListAdapter(this, hero.getStorageItems());
		
		//Toast.makeText(this, hero.getStorageItems()[0], Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO: Implement this method
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("退出");
		builder.setMessage("确定要退出么");
		builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface i, int b) {
					finish();
				}
			});
		builder.setNegativeButton("否", null);
		builder.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO: Implement this method
		switch(item.getItemId()) {
			case R.id.back: {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("返回角色选择菜单");
				builder.setMessage("确定要返回么");
				builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface i, int b) {
						//clearAll();
						Intent in = new Intent(MainActivity.this, CharacterSelectActivity.class);
						startActivity(in);
						finish();
					}
				});
				builder.setNegativeButton("否", null);
				builder.show();
				break;
			}
				
			case R.id.total: {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("物品栏");
				ListView l = new ListView(this);
				l.setAdapter(storageAdapter);
				l.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
						final int i = (int)l.getItemAtPosition(pos);
						AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
						builder.setTitle(Item.ITEMS.get(i).name);
						builder.setNegativeButton("装备", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface in, int b) {
								if(Item.ITEMS.get(i).equipable) {
									//equip
								} else {
									Toast.makeText(MainActivity.this, "不是装备...", Toast.LENGTH_SHORT).show();
								}
							}
						});
						builder.setPositiveButton("关闭", null);
						builder.show();
					}
				});
				builder.setView(l);
				builder.setNegativeButton("关闭", null);
				builder.show();
				break;
			}
			
			case R.id.power: {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Power");
				//builder.setMessage("ATK:" + level*2 + "\nHP:" + level * 4);
				builder.setPositiveButton("Close", null);
				builder.show();
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void btnClick(View v) {
		if (hero.earnExp(hero.level>>4<16?1:hero.level>>4)) {
			t.setText("Level:"+hero.level);
		}
		if (Math.random() < 0.1) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("发现新物品");
			builder.setMessage("没用的垃圾*1");
			builder.setNegativeButton("扔了", null);
			builder.setPositiveButton("捡起", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface i, int b) {
						hero.addItem(0);
						storageAdapter.notifyDataSetChanged();
					}
				});
			builder.show();
			
			
		}
		pb.setProgress((int)((float)hero.exp/(float)hero.needExp*100));
		exP.setText("EXP:"+hero.exp+"/"+hero.needExp);
	}
	
	void clearTotalCounter() {
		SharedPreferences sh = getSharedPreferences("info", Context.MODE_PRIVATE);
		SharedPreferences.Editor e = sh.edit();
		e.putInt("total", 0);
		e.commit();
	}
	
	
	
	
	class StorageListAdapter extends ArrayAdapter<Integer>
	{
		public StorageListAdapter(Context c, Integer[] values) {
			super(c, R.layout.storage_content, values);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inf = LayoutInflater.from(getContext());
			View v = inf.inflate(R.layout.storage_content, parent, false);
			
			TextView name = (TextView)v.findViewById(R.id.itemName);
			TextView des = (TextView)v.findViewById(R.id.itemDescription);
			
			name.setText(Item.ITEMS.get(getItem(position)).name);
			switch(Item.ITEMS.get(getItem(position)).level) {
				case 1:
					name.setTextColor(Color.GRAY);
					break;
					
				case 2:
					name.setTextColor(Color.GREEN);
					break;
			}
			des.setText(Item.ITEMS.get(getItem(position)).descrpition);
			return v;
		}
	}
}
